package com.mavenq.fly.utils.excelv3;

/**
 * 导入错误信息详情
 */
public class ImportErrorDetail {
    private final int rowIndex;        // 行索引（0-based）
    private final int columnIndex;     // 列索引（0-based）
    private final String sheetName;    // 工作表名称
    private final String fieldName;    // 字段名称
    private final Object cellValue;    // 单元格值
    private final String errorMessage; // 错误信息
    private final ErrorType errorType; // 错误类型

    public enum ErrorType {
        VALIDATION_ERROR,   // 校验错误
        DATA_FORMAT_ERROR,  // 数据格式错误
        REQUIRED_ERROR,     // 必填错误
        BUSINESS_ERROR      // 业务逻辑错误
    }

    public ImportErrorDetail(int rowIndex, int columnIndex, String sheetName,
                             String fieldName, Object cellValue,
                             String errorMessage, ErrorType errorType) {
        this.rowIndex = rowIndex;
        this.columnIndex = columnIndex;
        this.sheetName = sheetName;
        this.fieldName = fieldName;
        this.cellValue = cellValue;
        this.errorMessage = errorMessage;
        this.errorType = errorType;
    }

    // Getter 方法
    public int getRowIndex() {
        return rowIndex;
    }

    public int getColumnIndex() {
        return columnIndex;
    }

    public String getSheetName() {
        return sheetName;
    }

    public String getFieldName() {
        return fieldName;
    }

    public Object getCellValue() {
        return cellValue;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public ErrorType getErrorType() {
        return errorType;
    }

    /**
     * 获取Excel位置信息（用户友好）
     */
    public String getExcelPosition() {
        int excelRow = rowIndex + 1;
        String excelColumn = toExcelColumnName(columnIndex);
        return String.format("第%d行第%s列", excelRow, excelColumn);
    }

    /**
     * 获取完整错误信息
     */
    public String getFullErrorMessage() {
        return String.format("%s %s: %s (字段: %s, 值: %s)",
                getExcelPosition(), errorType, errorMessage,
                fieldName, cellValue != null ? cellValue : "空值");
    }

    private static String toExcelColumnName(int columnIndex) {
        StringBuilder sb = new StringBuilder();
        while (columnIndex >= 0) {
            sb.append((char) ('A' + (columnIndex % 26)));
            columnIndex = (columnIndex / 26) - 1;
        }
        return sb.reverse().toString();
    }
}